package com.yungu.swift.assets.driver.thread;

import com.yungu.swift.assets.driver.model.dto.DriverMessageDto;
import com.yungu.swift.common.model.dto.CommonMessageStatusDto;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.MagicConstant;
import com.yungu.swift.socket.push.model.PushContent;
import com.yungu.swift.socket.push.service.PushService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 添加司机消息后续处理：添加中间表及推送消息
 *
 * @version 1.0
 * @date 2017/12/29
 */
@Slf4j
public class DriverMessageThread implements Runnable {

    private PushService pushService;

    private CommonMessageStatusService commonMessageStatusService;

    private DriverMessageDto driverMessageDto;

    private List<String> driverUuids;

    /**
     * 自定义构造函数
     *
     * @param pushService
     * @param commonMessageStatusService
     * @param driverMessageDto
     * @param driverUuids
     */
    public DriverMessageThread(PushService pushService, CommonMessageStatusService commonMessageStatusService, DriverMessageDto driverMessageDto, List<String> driverUuids) {
        this.pushService = pushService;
        this.commonMessageStatusService = commonMessageStatusService;
        this.driverMessageDto = driverMessageDto;
        this.driverUuids = driverUuids;
    }


    @Override
    public void run() {
        if (driverUuids != null && !driverUuids.isEmpty()) {
            //插入t_common_message_status
            List<CommonMessageStatusDto> dtos = driverUuids.stream().map(driverUuid -> {
                CommonMessageStatusDto dto = new CommonMessageStatusDto();
                dto.setUuid(StringUtils.buildUUID());
                dto.setMessageUuid(driverMessageDto.getUuid());
                dto.setTargetUuid(driverUuid);
                dto.setStatus(CommonConstant.MSG_STATUS_NOREAD);
                dto.setType(CommonMessageStatusDto.TYPE_SYSTEM);
                dto.setBusinessType(driverMessageDto.getBusinessType());
                dto.setTargetType(CommonConstant.DRIVER);
                dto.setAppid(driverMessageDto.getAppid());
                return dto;
            }).collect(Collectors.toList());

            commonMessageStatusService.addBatch(dtos);
            //推送
            try {
                Map<String, Object> map = MapUtils.build(8);
                map.put("title", "系统消息");
                if (MagicConstant.INT_TWO == driverMessageDto.getSendType()) {
                    map.put("title", driverMessageDto.getTitle());
                }
                map.put("msgUuid", driverMessageDto.getUuid());
                //1文本2链接
                map.put("msgSendType", driverMessageDto.getSendType());
                map.put("linkUrl", driverMessageDto.getLinkUrl());
                map.put("content", driverMessageDto.getContent());
                PushContent pushContent = PushContent.newInstance4Dri(driverMessageDto.getAppid(),
                        PushContent.SYSTEM_MESSAGE_NOTICE, map);
                pushService.sendMessageByList(driverUuids, pushContent);
            } catch (Exception e) {
                log.error("----push error", e);
            }
        }
    }
}
